﻿using Carter;
using DDD.Application;
using DDD.Core.DependencyInjection;
using DDD.Extensions;
using DDD.GPT;
using DDD_Extensions;
using System.Reflection;
using Volo.Abp.Modularity;
using DDD_Extensions.Options;
using DDD.Common.Const;

namespace DDD.Api.Modules;

[DependsOn(typeof(ApplicationModule))]
public sealed class StartupModule : AbpModule
{

    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.Configure<OAuthSchemeOption>(option =>
        {
            option.DefaultAuthenticateScheme = AuthenticationConst.OAuth.DEFAULT;
            option.DefaultSignInScheme = AuthenticationConst.Cookie.DEFAULT;
            option.DefaultChallengeScheme = AuthenticationConst.OAuth.DEFAULT;
            option.CookieScheme = AuthenticationConst.Cookie.DEFAULT;
            option.OAuthScheme = AuthenticationConst.OAuth.DEFAULT;
            option.OAuthOption = new OAuthOption();
            context.Services.GetConfiguration().GetSection(nameof(AppSettingsConst.OAuth)).Bind(option.OAuthOption, op =>
            {
                op.BindNonPublicProperties = true;
            });
        });

    }

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services
            .AddChatGpt(context.Services.GetConfiguration())
            .AddDefaultSwagger("v1", "DDD", Assembly.GetExecutingAssembly())
            .AddEndpointsApiExplorer()
            .AddHttpContextAccessor()
            .AddCarter()
            .AddProblemDetails()
            .AddHttpClient()
            .AddExceptionHandler<GlobalExceptionHandler>()
            .AddOAuthAuthentication(IOC.GetOption<OAuthSchemeOption>())
            .AddControllers();
    }

}

